package com.qf.controller.servlet;

import com.qf.domain.Goods;
import com.qf.domain.PageBean;
import com.qf.service.GoodsService;
import com.qf.service.impl.GoodsServiceImpl;
import com.qf.utils.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author YDW
 * projectName:shopping
 * time:2022/4/19 17:33
 * description:
 */
@WebServlet("/goodsservlet")
public class GoodsServlet extends BaseServlet{
    GoodsService goodsService=new GoodsServiceImpl();
    public String getGoodsListByTypeId(HttpServletRequest request, HttpServletResponse response){
        //获取分类id
        int typeId = Integer.parseInt(request.getParameter("typeId"));
        //获取当前页码和每页显示数量
        String pageNumstr = request.getParameter("pageNum");
        String pageSizestr = request.getParameter("pageSize");
        int pageNum=1;
        int pageSize=2;
        if(!StringUtils.isEmpty(pageNumstr)){
            pageNum=Integer.parseInt(pageNumstr);
        }
        if(!StringUtils.isEmpty(pageSizestr)){
            pageSize=Integer.parseInt(pageSizestr);
        }
        PageBean pageBean=goodsService.findByPage(typeId,pageNum,pageSize);
        request.setAttribute("pageBean",pageBean);
        request.setAttribute("typeId",typeId);
        return "forward:/goodsList.jsp";
    }
    public String getGoodsById(HttpServletRequest request, HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));
        Goods goods=goodsService.findGoodsById(id);
        request.setAttribute("goods",goods);
        return "forward:/goodsDetail.jsp";
    }
}
